<?php require VIEWPATH . ADMIN_DIR . DIRECTORY_SEPARATOR . 'header.php' ?>
<style type="text/css">
	/* #import {
		position: relative;
		top: 1.4px;
	}
	#import-seats {
		position: relative;
		top: 1.4px;
	} */
	th {
		text-align: center;
		background-color: #6FB3E0;
		color: white;
	}
	td {
		text-align: center;
		vertical-align: middle !important;
		background-color: white !important;
	}
	.outer {
		text-align: center;
	}
	.btn-check {
		color: #3DA4FF;
	}
	.table {
		width: inherit;
	}
	.item-table {
		float: left;
	}
	.input-exam {
        margin-left: 8%;
    }

</style>
<div class="page-header">
	<h1>
		考试日程表
		<!-- 教师权限不可编辑 -->
		<?php if($rmxUser['user_type'] != USER_TYPE_TEACHER): ?>
			<a href="<?=admin_url('contents/exam_data')?>" class="btn btn-info a-custom">
				<i class="ace-icon fa fa-chain"></i> 数据管理页面
			</a>
			<!-- 同步学校不可添加数据 -->
			<?php if(! $rmxSchool['school_from']): ?>
				<span class="dropdown dropdown-custom">
					<div class="btn btn-info btn-dropdown"  data-toggle="dropdown">
						<i class="ace-icon fa fa-database"></i> 考场安排模板导入与导出&nbsp;&nbsp;<i class="ace-icon fa fa-caret-down"></i>
					</div>
					<ul class="dropdown-menu">
						<li>
							<a href="javascript:" id="import">
								<i class="ace-icon fa fa-upload"></i> 导入考场安排数据
							</a>
						</li>
						<li>
							<a href="https://resource.rongmixiao.com/2019/12/18/8ff55ef56872420387b00b48447eabd1.xls" target="_blank">
								<i class="ace-icon fa fa-download"></i> 导出考场安排模板
							</a>
						</li>
					</ul>
				</span>
				<span class="dropdown dropdown-custom">
					<div class="btn btn-info btn-dropdown"  data-toggle="dropdown">
						<i class="ace-icon fa fa-database"></i> 考场座位模板导入与导出&nbsp;&nbsp;<i class="ace-icon fa fa-caret-down"></i>
					</div>
					<ul class="dropdown-menu">
						<li>
							<a href="javascript:" id="import-seats">
								<i class="ace-icon fa fa-upload"></i> 导入考场座位数据
							</a>
						</li>
						<li>
							<a href="https://resource.rongmixiao.com/2019/12/18/6df6b2678a8092739117a15ef3a49335.xls" target="_blank">
								<i class="ace-icon fa fa-download"></i> 导出考场座位模板
							</a>
						</li>
					</ul>
				</span>
			<?php endif ?>	
		<?php endif ?>	
	</h1>
</div>
<div class="col-xs-12 content-filter">
	<!-- 筛选 -->
	<div class="widget-box">
		<div class="widget-header">
			<h4 class="widget-title">内容筛选</h4>
			<div class="widget-toolbar">
				<a href="#" data-action="collapse">
					<i class="ace-icon fa fa-chevron-up"></i>
				</a>
				<a href="#" data-action="close">
					<i class="ace-icon fa fa-times"></i>
				</a>
			</div>
		</div>
		<div class="widget-body">
			<div class="widget-main">
				<div class="form-inline" id="data-search">
					<div class="form-inline keyword-search-div" id="div-grade">
						<label class="keyword-search-label">年级</label>
						<div class="keyword-search-box">
							<a href="javascript:;" class="keyword-search-keyword active" data-id="0">全部</a>
							<?php if(isset($data['grades'])): ?>
								<?php foreach($data['grades'] as $grade): ?>
									<a href="javascript:;" class="keyword-search-keyword" data-id="<?=$grade['id']?>"><?=$grade['grade_name']?></a>
								<?php endforeach; ?>
							<?php endif ?>
						</div>
					</div>
					<div class="form-inline keyword-search-div" id="div-subject">
						<label class="keyword-search-label">科目</label>
						<div class="keyword-search-box">
							<a href="javascript:;" class="keyword-search-keyword active" data-id="0">全部</a>
							<?php if(isset($data['lessons'])): ?>
								<?php foreach($data['lessons'] as $lesson): ?>
									<a href="javascript:;" class="keyword-search-keyword" data-id="<?=$lesson['id']?>"><?=$lesson['lesson_name']?></a>
								<?php endforeach; ?>
							<?php endif ?>
						</div>
					</div>
					<div class="form-inline keyword-search-div" id="div-type">
						<label class="keyword-search-label">类型</label>
						<div class="keyword-search-box">
							<a href="javascript:;" class="keyword-search-keyword active" data-id="0">全部</a>
							<?php if(isset($data['types'])): ?>
								<?php foreach($data['types'] as $english => $chinese): ?>
									<a href="javascript:;" class="keyword-search-keyword" data-id="<?=$english?>"><?=$chinese?></a>
								<?php endforeach; ?>
							<?php endif ?>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<div class="outer">
	<?php if(isset($data['exams']) && !empty($data['exams'])): ?>
		<?php foreach ($data['exams'] as $key => $exams) : ?>
			<div class="tab-content no-border padding-24 item-table">
				<table class="table table-striped table-bordered">
					<thead>
						<tr>
							<th colspan="<?= count($exams)?>"><?= $key ?> （<?= $exams[0]['week'] ?>）</th>
						</tr>
					</thead>
					<tbody>
						<tr>
							<?php 
								$part = array(
									'morning' => 0,
									'afternoon' => 0,
									'evening' => 0
								);
								foreach($exams as $exam) {
									if ('上午' === $exam['part_of_day']) {
										$part['morning'] ++;
									} else if('下午' === $exam['part_of_day']) {
										$part['afternoon'] ++;
									} else {
										$part['evening'] ++;
									}
								}
								if ($part['morning'] > 0) {
									echo "<th colspan={$part['morning']}>上午</th>";
								}
								if ($part['afternoon'] > 0) {
									echo "<th colspan={$part['afternoon']}>下午</th>";
								}
								if ($part['evening'] > 0) {
									echo "<th colspan={$part['evening']}>晚上</th>";
								}
							?>
						</tr>
						<tr>
							<?php foreach ($exams as $subKey => $exam) : ?>
								<td>
									第<?= $exam['timeslot'] + 1 ?>场
								</td>
							<?php endforeach ?>
						</tr>
						<tr>
							<?php foreach ($exams as $exam) : ?>
								<td>
									<?= $exam['begin'] ?> - <?= $exam['end'] ?>
									<br>
									<?= $exam['duration'] ?>分钟
								</td>
							<?php endforeach ?>
						</tr>
						<tr>
							<?php foreach ($exams as $exam) : ?>
								<td>
									<?= $exam['grade_name'] ?>
								</td>
							<?php endforeach ?>
						</tr>
						<tr>
							<?php foreach ($exams as $exam) : ?>
								<td>
									<?= $exam['type'] ?>
								</td>
							<?php endforeach ?>
						</tr>
						<tr>
							<?php foreach ($exams as $exam) : ?>
								<td>
									<?= $exam['lesson_name'] ?>
								</td>
							<?php endforeach ?>
						</tr>
						<tr>
							<?php foreach ($exams as $exam) : ?>
								<td>
									<a class="btn-check" href="#<?= $exam['id'] ?>" data-toggle="modal"><i class="fa fa-edit"></i>修改进场时间</a>
								</td>
								<!-- 考试进场时间弹窗 -->
								<div id="<?= $exam['id'] ?>" class="modal fade">
									<div class="modal-dialog">
										<div class="modal-content">
											<div class="modal-header no-padding">
												<div class="table-header">
													<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
														<span class="white">&times;</span>
													</button>
													<span>设置考试进场时间</span>
												</div>
											</div>
											<div class="modal-body no-padding">
												<div class="row">
													<div class="col-xs-12">
														<form class="form-horizontal mt10">
															<input type="hidden" name="id" />
															<div class="alert alert-success col-sm-10 input-exam">
																考试前多少分钟可以进场：<input type="number" name='beforetime' value="<?= $exam['exam_entry_time_before'] ?>">
																<br>
																考试后多少分钟禁止进场：<input type="number" name='aftertime' value="<?= $exam['exam_entry_time_after'] ?>">
															</div>
														</form>
													</div>
												</div>
											</div>
											<div class="modal-footer no-margin-top" >
												<a href="javascript:;" class="btn btn-sm btn-primary"  onclick="edit_entry_time('<?= $exam['id'] ?>')">
													<i class="ace-icon fa fa-check"></i> 保存
												</a>
											</div>
										</div>
									</div>
								</div>
							<?php endforeach ?>
						</tr>
						<tr>
							<?php foreach ($exams as $exam) : ?>
								<td>
									<a class="btn-check" href="<?=admin_url('contents/exam_room/') . $exam['id'] ?>"><i class="fa fa-table"></i> 查看座位</a>
								</td>
							<?php endforeach ?>
						</tr>
					</tbody>
				</table>
			</div>
		<?php endforeach ?>
	<?php else: ?>
		<div class="col-xs-11" style="font-size: 20px;margin-top: 60px;color: #A8A8A8;">没有查询结果</div>
	<?php endif ?>
</div>

<?php require VIEWPATH . ADMIN_DIR . DIRECTORY_SEPARATOR . 'footer.php' ?>
<script type="text/javascript">
$(function() {
	// 筛选框选中默认值
	var xGradeIdSelect = <?= $data['xGradeIdSelect'] ?>;
	var xLessonIdSelect = <?= $data['xLessonIdSelect'] ?>;
	var typeSelect = '<?= $data['typeSelect'] ?>';
	if(xGradeIdSelect) {
		var gradeElement = $("#div-grade [data-id=" + xGradeIdSelect + "]");
		gradeElement.parent().find(".keyword-search-keyword").removeClass("active");
		gradeElement.addClass("active");
	}
	if(xLessonIdSelect) {
		var lessonElement = $("#div-subject [data-id=" + xLessonIdSelect + "]");
		lessonElement.parent().find(".keyword-search-keyword").removeClass("active");
		lessonElement.addClass("active");
	}
	if(typeSelect) {
		var typeElement = $("#div-type [data-id=" + typeSelect + "]");
		typeElement.parent().find(".keyword-search-keyword").removeClass("active");
		typeElement.addClass("active");
	}
	// 点击关键字
	$("body").on("click", ".keyword-search-keyword", function(){
		var id = $(this).data("id");
		// 高亮当前点击对象
		$(this).parent().find(".keyword-search-keyword").removeClass("active");
		$(this).addClass("active");
		// 筛选
		var xGradeId = $("#div-grade .active").data('id');
		var xLessonId = $("#div-subject .active").data('id');
		var type = $("#div-type .active").data('id');
		window.location.href="<?=admin_url('contents/exam?grade_id=')?>" + xGradeId  + "&lesson_id=" + xLessonId + "&type=" + type;
	});
	// 导入安排
	var upFromurl = '<?=UP_FORM_URL?>';
	var signature = '<?=$signature?>';
	var policy = '<?=$policy?>';
	$("#import").uploader({
		"server": upFromurl,
		"authorization": signature,
		"policy": policy,
		// 只支持上传 xls
		"accept": "xls",
		"callback": function(response) {
			// 上传后的文件名
			var fileLink = upyunUrl + response.url;
			ajax_post("<?= admin_url('import/import_exam') ?>", { fileLink: fileLink}, function(res) {
				// 刷新页面
				location.reload();
			});
		}
	});
	// 导入座位
	$("#import-seats").uploader({
		"server": upFromurl,
		"authorization": signature,
		"policy": policy,
		// 只支持上传 xls
		"accept": "xls",
		"callback": function(response) {
			// 上传后的文件名
			var fileLink = upyunUrl + response.url;
			ajax_post("<?= admin_url('import/import_exam_seats') ?>", { fileLink: fileLink}, function(res) {
				// 刷新页面
				location.reload();
			});
		}
	});
	// 设置选择文件按钮大小
		setTimeout(function(){
		$(".webuploader-pick").next().width(158).height(32);
	}, 100);
});
function edit_entry_time(roomId)
{
	var begintime = $('#' + roomId + ' input[name=beforetime]').val();
	var aftertime = $('#' + roomId + ' input[name=aftertime]').val();
	var data = {
		"id": roomId,
		"before_time": begintime,
		"after_time": aftertime
	}
	var actionUrl = "<?=admin_url('contents/edit_exam_entry_time')?>";
	ajax_post(actionUrl, data, function(res){
		window.location.reload();
	})
}
</script>
